<?php

namespace app\admin\service\statistics\traits;

use app\admin\service\statistics\completions\CellCompletion;
use app\admin\service\statistics\completions\TimesCompletion;

/**
 * Trait CompletionTimesTrait
 * @package app\admin\service\statistics\traits
 */
trait CompletionTrait
{
    /**
     * 单元格补齐
     * @param array $data
     * @param CellCompletion $completion
     * @return array
     */
    protected function completionByCellCompletion($data, CellCompletion $completion)
    {
        $time = $completion->getTime();

        if (isset($data[0]) && isset($data[0]['value'])) {


            if ($time <= time()) {
                return $data;
            }

        }

        return [
            [
                'value' => $this->getDefaultValueOfTime($time)
            ]
        ];
    }

    /**
     * 横轴时间补齐
     * @param array $data
     * @param TimesCompletion $completion
     * @return array
     */
    protected function completionByTimesCompletion($data, TimesCompletion $completion)
    {
        $times = $completion->getTimes();

        $time_arr = array_map(fn($item) => date('Y-m-d', $item['s_time']), $times);

        return array_completion($data, ['date' => $time_arr], fn($field, $date) => [$field => $date, 'value' => $this->getDefaultValueOfTime(strtotime($date)),]);
    }

    protected function getDefaultValueOfTime($time)
    {
        if ($time > time()) {
            return '';
        }

        return $this->getDefaultValue();
    }

    protected function getDefaultValue()
    {
        return '';
    }
}